package com.vzoom.logdesensitization.strategyimpl;

import com.vzoom.logdesensitization.DesensitizationStrategy;
import com.vzoom.logdesensitization.util.StarUtil;

public class BirthdayDesensitizationStrategy implements DesensitizationStrategy {
    @Override
    public String desensitization(Object origin) {
        String toString = String.valueOf(origin);
        if (origin == null) {
            return toString;
        }
        int length = toString.length();
        int maxLength = length - 4;
        int index = 0;
        boolean find = false;
        if (maxLength > 3) {
            char c1, c2, c3, c4;
            while (index < maxLength) {
                c1 = toString.charAt(index++);
                c2 = toString.charAt(index++);
                c3 = toString.charAt(index++);
                c4 = toString.charAt(index++);
                if (Character.isDigit(c1) && Character.isDigit(c2) && Character.isDigit(c3) && Character.isDigit(c4)) {
                    find = true;
                    break;
                }
            }
        }
        if (find) {
            return StarUtil.getStar(4) + toString.substring(index, length);
        } else {
            return toString;
        }
    }
}
